﻿using BUGCOME.Infrastructure.Attribute;
using BUGCOME.Model;
using BUGCOME.Model.Log;
using BUGCOME.Model.Log.Dto;
using BUGCOME.Repository;
using BUGCOME.ServiceCore;
using BUGCOME.ServiceCore.Services.IServices;
using System.Threading.Tasks;

namespace BUGCOME.Service.Services
{
    /// <summary>
    /// 操作日志
    /// </summary>
    [AppService(ServiceType = typeof(IOperLogService), ServiceLifetime = LifeTime.Transient)]
    public class OperLogService : BaseService<OperLog>, IOperLogService
    {
        /// <summary>
        /// 新增操作日志操作
        /// </summary>
        /// <param name="operLog">日志对象</param>
        public async Task InsertOperlog(OperLog operLog)
        {
            if (operLog.OperParam != null && operLog.OperParam.Length >= 1000)
            {
                operLog.OperParam = operLog.OperParam[..1000];
            }
            //operLogRepository.AddOperLog(operLog);
            await InsertAsync(operLog);
        }

        /// <summary>
        /// 查询系统操作日志集合
        /// </summary>
        /// <param name="oper">操作日志对象</param>
        /// <returns>操作日志集合</returns>
        public async Task<PagedInfo<OperLog>> SelectOperLogListAsync(OperLogQueryDto oper)
        {
            var exp = Expressionable.Create<OperLog>();
            exp.AndIF(oper.BeginTime == null, it => it.OperTime >= DateTime.Now.ToShortDateString().ParseToDateTime());
            exp.AndIF(oper.BeginTime != null, it => it.OperTime >= oper.BeginTime && it.OperTime <= oper.EndTime);
            exp.AndIF(oper.Title.IfNotEmpty(), it => it.Title.Contains(oper.Title));
            exp.AndIF(oper.OperName.IfNotEmpty(), it => it.OperName.Contains(oper.OperName));
            exp.AndIF(oper.BusinessType != null, it => it.BusinessType == oper.BusinessType);
            exp.AndIF(oper.Status != null, it => it.Status == oper.Status);
            exp.AndIF(oper.OperParam != null, it => it.OperParam.Contains(oper.OperParam));
            exp.AndIF(oper.BusinessTypes != null, it => oper.BusinessTypes.Contains(it.BusinessType));

            var result = await Queryable()
                .Where(exp.ToExpression())
                .OrderBy(x => x.OperId, OrderByType.Desc)
                .ToPageAsync(oper);
            return result;
        }

        /// <summary>
        /// 清空操作日志
        /// </summary>
        public async Task CleanOperLog()
        {
          await TruncateAsync();
        }

        /// <summary>
        /// 批量删除系统操作日志
        /// </summary>
        /// <param name="operIds">需要删除的操作日志ID</param>
        /// <returns>结果</returns>
        public async Task<int> DeleteOperLogByIdsAsync(long[] operIds)
        {
            return await Deleteable().Where(u=>operIds.Contains(u.OperId)).ExecuteCommandAsync();
        }

        /// <summary>
        /// 查询操作日志详细
        /// </summary>
        /// <param name="operId">操作ID</param>
        /// <returns>操作日志对象</returns>
        public async Task<OperLog> SelectOperLogByIdAsync(long operId)
        {
            return await GetByIdAsync(operId);
        }

       
    }
}